package com.satan.juc.demo26;

import java.util.concurrent.Exchanger;

/**
 * @Author:     Demon
 * @Date:       2024/3/30 22:12
 * @Description:
 **/
public class ExchangeExample {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();

        // 线程A
        new Thread(() -> {
            try {
                String str = "由线程A提供";
                str = exchanger.exchange(str);
                System.out.println("线程A接收到: " + str);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        // 线程B
        new Thread(() -> {
            try {
                String str = "由线程B提供";
                str = exchanger.exchange(str);
                System.out.println("线程B接收到: " + str);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
